Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to generate a binary variable from a range of different dates*

    Hello

    I need to create a binary variable called precovid = 1/0 from 2 date variables defining the date of the covid diagnosis (covidfirst_date and that date – 365 days (date_1yrprecovid)
    Each individual has a different date for his/her covid diagnosis, and therefore a different date_1yrprecovid, and I need to code precovid =1 if the period is between date_1yrprecovid and covidfirst_date and 0 otherwise (period post covid).

    I have tried several ways and none worked
    Last one was trying to use the inrange command

    gen precovid=.
    replace precovid=1 if inrange(date_1yrprecovid , covidfirst_date) but still nothing….
    code
    Would appreciate any suggestion
    thanks a lot
    best
    Fernando

  • #2
    replace precovid=1 if inrange(date_1yrprecovid , covidfirst_date) but still nothing….
    -inrange()- expects 3 arguments. In your case, the variable containing the dates, the lower limit and the upper limit. If you cannot make progress, provide a data example using the dataex command (see FAQ Advice #12 for details).

    Comment


    • #3
      Hi Andrew. Thanks a lot for your response!
      I will try that and get back with the example. best

      Comment


      • #4
        I am sorry the request was not clear

        I want to evaluate the % admissions to the hospital in a period defined by 1 year before and after the diagnosis of covid of a group of patients. Some patients received a treatment at the time of diagnosis and others are controls and the idea is trying to evaluate if the % of admissions before and after the intervention changed as a result of the intervention.

        The problem is that each individual has a different date of covid diagnosis, and therefore different dates for the 1 year before and after it. I can identify the number of admissions they had before and after their diagnosis for those who had admissions based on the date of admission, but most of them were not admitted, so there is no date of admission (missing) for those.

        I wanted to generate a variable precovid =1 if the period for each individual is between his/her date_1yrprecovid and covidfirst_date and 0 otherwise (period post covid between covidfirst_date and 1yr post diagnosis) but not sure how to do that

        I am sending an example dataset with 50 observations and a few with pre or post covid admissions in case it is helpful
        thanks a lot
        best
        Fernando
        Attached Files

        Comment


        • #5
          Open the dataset in Stata, type

          Code:
          dataex
          and copy and paste the result here.

          Comment


          • #6
            Sorry, never used dataex. I hope it's correct. Thanks
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int covidfirst_date float(dateFU_1yr date_1yrprecovid dateofadmission) byte Treatment float(unplanned_PRE unplanned_adm unplanned_POST)
            22325 22690 21960     . 0 0 0 0
            22307 22672 21942     . 0 0 0 0
            22673 23038 22308     . 0 0 0 0
            22580 22945 22215     . 0 0 0 0
            22315 22680 21950     . 0 0 0 0
            22568 22933 22203     . 0 0 0 0
            22439 22804 22074     . 0 0 0 0
            22530 22895 22165     . 1 0 0 0
            22477 22842 22112     . 0 0 0 0
            22364 22729 21999     . 0 0 0 0
            22640 23005 22275     . 0 0 0 0
            22529 22894 22164     . 0 0 0 0
            22521 22886 22156     . 0 0 0 0
            22325 22690 21960     . 0 0 0 0
            22644 23009 22279     . 0 0 0 0
            22554 22919 22189     . 0 0 0 0
            22486 22851 22121     . 0 0 0 0
            22624 22989 22259     . 0 0 0 0
            22312 22677 21947 22627 0 0 0 0
            22592 22957 22227     . 0 0 0 0
            22503 22868 22138     . 0 0 0 0
            22639 23004 22274     . 0 0 0 0
            22477 22842 22112     . 0 0 0 0
            22417 22782 22052     . 0 0 0 0
            22502 22867 22137     . 0 0 0 0
            22564 22929 22199 22468 0 0 0 0
            22653 23018 22288     . 0 0 0 0
            22325 22690 21960     . 0 0 0 0
            22494 22859 22129     . 0 0 0 0
            22625 22990 22260     . 0 0 0 0
            22540 22905 22175 22777 0 0 0 0
            22337 22702 21972     . 0 0 0 0
            22729 23094 22364 22337 0 0 0 0
            22729 23094 22364 22342 0 0 0 0
            22480 22845 22115     . 0 0 0 0
            22426 22791 22061     . 0 0 0 0
            22643 23008 22278     . 0 0 0 0
            22578 22943 22213     . 0 0 0 0
            22513 22878 22148 22532 0 0 1 1
            22458 22823 22093     . 0 0 0 0
            22586 22951 22221     . 0 0 0 0
            22543 22908 22178     . 0 0 0 0
            22314 22679 21949     . 0 0 0 0
            22719 23084 22354     . 0 0 0 0
            22526 22891 22161 22807 0 0 0 0
            22306 22671 21941 22134 0 1 1 0
            22509 22874 22144     . 0 0 0 0
            22427 22792 22062     . 1 0 0 0
            22469 22834 22104     . 0 0 0 0
            22596 22961 22231     . 0 0 0 0
            end
            format %td covidfirst_date
            format %td dateFU_1yr
            format %td date_1yrprecovid
            format %td dateofadmission
            label values Treatment ox_home
            label def ox_home 0 "No", modify
            label def ox_home 1 "Yes", modify
            label values unplanned_adm unplanned
            label def unplanned 0 "No", modify
            label def unplanned 1 "Yes", modify

            Comment


            • #7
              I have tried reading your description several times and looking at your data, but your question still does not make sense to me.

              Each individual has a different date for his/her covid diagnosis, and therefore a different date_1yrprecovid, and I need to code precovid =1 if the period is between date_1yrprecovid and covidfirst_date and 0 otherwise (period post covid).
              What do you mean by "the period". I get that you have a first COVID diagnosis date (covidfirst_date) and one year before this (date_1yrprecovid). So are you referring to the date of admission as "the period". Perhaps you can illustrate what you mean by telling us what values the wanted variable takes for a couple of observations with the example data above.

              Comment


              • #8
                OK, maybe this explains it. Note that the variable will always be 0 for the control (those never admitted) and either 0/1 for those admitted.

                I want to evaluate the % admissions to the hospital in a period defined by 1 year before and after the diagnosis of covid of a group of patients. Some patients received a treatment at the time of diagnosis and others are controls and the idea is trying to evaluate if the % of admissions before and after the intervention changed as a result of the intervention
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int covidfirst_date float(dateFU_1yr date_1yrprecovid dateofadmission) byte Treatment float(unplanned_PRE unplanned_adm unplanned_POST)
                22325 22690 21960     . 0 0 0 0
                22307 22672 21942     . 0 0 0 0
                22673 23038 22308     . 0 0 0 0
                22580 22945 22215     . 0 0 0 0
                22315 22680 21950     . 0 0 0 0
                22568 22933 22203     . 0 0 0 0
                22439 22804 22074     . 0 0 0 0
                22530 22895 22165     . 1 0 0 0
                22477 22842 22112     . 0 0 0 0
                22364 22729 21999     . 0 0 0 0
                22640 23005 22275     . 0 0 0 0
                22529 22894 22164     . 0 0 0 0
                22521 22886 22156     . 0 0 0 0
                22325 22690 21960     . 0 0 0 0
                22644 23009 22279     . 0 0 0 0
                22554 22919 22189     . 0 0 0 0
                22486 22851 22121     . 0 0 0 0
                22624 22989 22259     . 0 0 0 0
                22312 22677 21947 22627 0 0 0 0
                22592 22957 22227     . 0 0 0 0
                22503 22868 22138     . 0 0 0 0
                22639 23004 22274     . 0 0 0 0
                22477 22842 22112     . 0 0 0 0
                22417 22782 22052     . 0 0 0 0
                22502 22867 22137     . 0 0 0 0
                22564 22929 22199 22468 0 0 0 0
                22653 23018 22288     . 0 0 0 0
                22325 22690 21960     . 0 0 0 0
                22494 22859 22129     . 0 0 0 0
                22625 22990 22260     . 0 0 0 0
                22540 22905 22175 22777 0 0 0 0
                22337 22702 21972     . 0 0 0 0
                22729 23094 22364 22337 0 0 0 0
                22729 23094 22364 22342 0 0 0 0
                22480 22845 22115     . 0 0 0 0
                22426 22791 22061     . 0 0 0 0
                22643 23008 22278     . 0 0 0 0
                22578 22943 22213     . 0 0 0 0
                22513 22878 22148 22532 0 0 1 1
                22458 22823 22093     . 0 0 0 0
                22586 22951 22221     . 0 0 0 0
                22543 22908 22178     . 0 0 0 0
                22314 22679 21949     . 0 0 0 0
                22719 23084 22354     . 0 0 0 0
                22526 22891 22161 22807 0 0 0 0
                22306 22671 21941 22134 0 1 1 0
                22509 22874 22144     . 0 0 0 0
                22427 22792 22062     . 1 0 0 0
                22469 22834 22104     . 0 0 0 0
                22596 22961 22231     . 0 0 0 0
                end
                format %td covidfirst_date
                format %td dateFU_1yr
                format %td date_1yrprecovid
                format %td dateofadmission
                label values Treatment ox_home
                label def ox_home 0 "No", modify
                label def ox_home 1 "Yes", modify
                label values unplanned_adm unplanned
                label def unplanned 0 "No", modify
                label def unplanned 1 "Yes", modify
                
                gen wanted= inrange(dateofadmission, date_1yrprecovid, covidfirst_date)
                Res.:

                Code:
                . l date_1yrprecovid covidfirst_date dateofadmission wanted , sep(0)
                
                     +--------------------------------------------+
                     | date_1y~d   covidfi~e   dateofa~n   wanted |
                     |--------------------------------------------|
                  1. | 15feb2020   14feb2021           .        0 |
                  2. | 28jan2020   27jan2021           .        0 |
                  3. | 28jan2021   28jan2022           .        0 |
                  4. | 27oct2020   27oct2021           .        0 |
                  5. | 05feb2020   04feb2021           .        0 |
                  6. | 15oct2020   15oct2021           .        0 |
                  7. | 08jun2020   08jun2021           .        0 |
                  8. | 07sep2020   07sep2021           .        0 |
                  9. | 16jul2020   16jul2021           .        0 |
                 10. | 25mar2020   25mar2021           .        0 |
                 11. | 26dec2020   26dec2021           .        0 |
                 12. | 06sep2020   06sep2021           .        0 |
                 13. | 29aug2020   29aug2021           .        0 |
                 14. | 15feb2020   14feb2021           .        0 |
                 15. | 30dec2020   30dec2021           .        0 |
                 16. | 01oct2020   01oct2021           .        0 |
                 17. | 25jul2020   25jul2021           .        0 |
                 18. | 10dec2020   10dec2021           .        0 |
                 19. | 02feb2020   01feb2021   13dec2021        0 |
                 20. | 08nov2020   08nov2021           .        0 |
                 21. | 11aug2020   11aug2021           .        0 |
                 22. | 25dec2020   25dec2021           .        0 |
                 23. | 16jul2020   16jul2021           .        0 |
                 24. | 17may2020   17may2021           .        0 |
                 25. | 10aug2020   10aug2021           .        0 |
                 26. | 11oct2020   11oct2021   07jul2021        1 |
                 27. | 08jan2021   08jan2022           .        0 |
                 28. | 15feb2020   14feb2021           .        0 |
                 29. | 02aug2020   02aug2021           .        0 |
                 30. | 11dec2020   11dec2021           .        0 |
                 31. | 17sep2020   17sep2021   12may2022        0 |
                 32. | 27feb2020   26feb2021           .        0 |
                 33. | 25mar2021   25mar2022   26feb2021        0 |
                 34. | 25mar2021   25mar2022   03mar2021        0 |
                 35. | 19jul2020   19jul2021           .        0 |
                 36. | 26may2020   26may2021           .        0 |
                 37. | 29dec2020   29dec2021           .        0 |
                 38. | 25oct2020   25oct2021           .        0 |
                 39. | 21aug2020   21aug2021   09sep2021        0 |
                 40. | 27jun2020   27jun2021           .        0 |
                 41. | 02nov2020   02nov2021           .        0 |
                 42. | 20sep2020   20sep2021           .        0 |
                 43. | 04feb2020   03feb2021           .        0 |
                 44. | 15mar2021   15mar2022           .        0 |
                 45. | 03sep2020   03sep2021   11jun2022        0 |
                 46. | 27jan2020   26jan2021   07aug2020        1 |
                 47. | 17aug2020   17aug2021           .        0 |
                 48. | 27may2020   27may2021           .        0 |
                 49. | 08jul2020   08jul2021           .        0 |
                 50. | 12nov2020   12nov2021           .        0 |
                     +--------------------------------------------+
                
                .

                Comment


                • #9
                  Thanks again Andrew
                  Unfortunately that only identifies as 1 all admissions within the date range. I am guessing that my question does not have a straightforward answer because what I want is to define a period pre and postcovid for each patient, whether they were admitted or not during the period defined by each patient's dates pre and post their diagnosis, and since most patients were not admitted, I do not have a date for them..
                  I appreciate your kind help and I will keep trying...
                  best
                  Fernando

                  Comment


                  • #10
                    Originally posted by fernando rubinstein View Post
                    I am guessing that my question does not have a straightforward answer because what I want is to define a period pre and postcovid for each patient, whether they were admitted or not during the period defined by each patient's dates pre and post their diagnosis, and since most patients were not admitted, I do not have a date for them.
                    Are you following any previous related work on this? It is possible to generate a duration variable between first diagnosis and admission for the treated individuals and then do a propensity score matching to match each individual in the control group with an individual in the treatment group. Then define the theoretical admission date for an individual in the control group as their date of first diagnosis plus the duration of the closest matched individual in the treatment group. However, whether your peers will buy this is a completely different matter. See

                    Code:
                    ssc install psmatch2, replace
                    help psmatch2

                    Comment


                    • #11
                      Great, again, thanks a lot for your help!!! I will try this
                      best

                      Comment

                      Working...
                      X